package com.p2p.handler;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;

// 拦截 WebSocket 请求路径（根据实际项目调整，如 /peerjs-server/*）
@Order(1)
@Component // 让 Spring 扫描并注册过滤器
@WebFilter(urlPatterns = "/peerjs/*")
public class UriFixFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 仅处理 HTTP 请求
        if (request instanceof HttpServletRequest) {
            // 用修复后的请求包装原始请求
            HttpServletRequest fixedRequest = new FixedUriRequestWrapper((HttpServletRequest) request);
            chain.doFilter(fixedRequest, response);
        } else {
            chain.doFilter(request, response);
        }
    }
}
